﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SynoManager.GUI;
using SynoManager.DownloadStation.Plugins;

namespace SynoManager.DownloadStation
{
    public partial class QueuePreferencesPanel : PreferencesPanel
    {
        private readonly DownloadStationService service;
        private readonly QueueManager plugin;

        public QueuePreferencesPanel()
        {
            InitializeComponent();
        }

        public QueuePreferencesPanel(DownloadStationService service)
            :this()
        {
            this.service = service;
            this.plugin = (QueueManager)service.Plugins[typeof (QueueManager)];

            if (this.service.Device.IsAdmin)
            {
                maxTasksNumericUpDown.Value = service.Configuration.MaxActiveTasks;

                switch (service.Configuration.ProcessOrder)
                {
                    case ProcessOrder.Request:
                        processOrderRequestRadioButton.Checked = true;
                        break;
                    case ProcessOrder.User:
                        processOrderUserRadioButton.Checked = true;
                        break;
                    default:
                        throw new ArgumentOutOfRangeException();
                }

                downloadTimeFromPicker.Value = DateTime.Today.Add(service.Configuration.DownloadTimeFrom);
                downloadTimeToPicker.Value = DateTime.Today.Add(service.Configuration.DownloadTimeTo);
            }
            else
            {
                maxTasksNumericUpDown.Enabled = false;
                processOrderRequestRadioButton.Enabled = false;
                processOrderUserRadioButton.Enabled = false;
                downloadTimeFromPicker.Enabled = false;
                downloadTimeToPicker.Enabled = false;
            }



            pauseAllOnStartCheckBox.Checked = plugin.PauseAllOnStart;
            resumeAllOnCloseCheckBox.Checked = plugin.ResumeAllOnClose;
        }

        public override void Save()
        {
            service.Configuration.MaxActiveTasks = (int)maxTasksNumericUpDown.Value;

            service.Configuration.DownloadTimeFrom = downloadTimeFromPicker.Value.TimeOfDay;
            service.Configuration.DownloadTimeTo = downloadTimeToPicker.Value.TimeOfDay;
            
            plugin.PauseAllOnStart = pauseAllOnStartCheckBox.Checked;
            plugin.ResumeAllOnClose = resumeAllOnCloseCheckBox.Checked;
        }
    }
}
